package com.sky.controller.admin;

import com.sky.result.Result;
import com.sky.service.ReportService;
import com.sky.vo.OrderReportVO;
import com.sky.vo.SalesTop10ReportVO;
import com.sky.vo.TurnoverReportVO;
import com.sky.vo.UserReportVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;
import java.time.LocalDateTime;

@RestController
@RequestMapping("/admin/report")
@Api(tags = "数据统计相关接口")
@Slf4j
public class ReportController {
    @Autowired
    private ReportService reportService;
    @GetMapping("/turnover")
    @ApiOperation(value = "营业额统计")
    public Result<TurnoverReportVO> turoverStatists(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate beginTime, @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endTime) {
        log.info("营业统计");
        TurnoverReportVO result = reportService.turnoverStatists(beginTime, endTime);
        return Result.success(result);
    }
    @GetMapping("/user")
    @ApiOperation("用户统计")
    public Result<UserReportVO> userStatists(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate beginTime, @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endTime) {
        log.info("用户统计");
        UserReportVO result = reportService.userStatists(beginTime, endTime);
        return Result.success(result);
    }

    /**
     * 订单统计
     * @param beginTime
     * @param endTime
     * @return
     */
    @GetMapping("/order")
    @ApiOperation("订单统计")
    public Result<OrderReportVO> orderStatists(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate beginTime, @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endTime) {
        log.info("订单统计");
        OrderReportVO result = reportService.orderStatists(beginTime, endTime);
        return Result.success(result);
    }

    /**
     * 销量统计
     * @param beginTime
     * @param endTime
     * @return
     */
    @GetMapping("/sale")
    @ApiOperation("销量统计")
    public Result<SalesTop10ReportVO> saleStatists(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate beginTime, @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endTime) {
        log.info("销量统计");
        SalesTop10ReportVO result = reportService.saleStatists(beginTime, endTime);
        return Result.success(result);
    }

    /**
     * 导出报表
     * @param response
     */
    @GetMapping("/export")
    @ApiOperation("导出报表")
    public  void exportExcel(HttpServletResponse response) {
        log.info("导出报表");
        reportService.exportExcel(response);
    }
}
